package com.wanghaixin.controller;

import com.wanghaixin.dto.DataGridResult;
import com.wanghaixin.dto.QueryDTO;
import com.wanghaixin.pojo.CarMake;
import com.wanghaixin.pojo.CarModel;
import com.wanghaixin.service.CarMakeService;
import com.wanghaixin.service.CarModelService;
import com.wanghaixin.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Auther: 王海新
 * @Date: 2020/11/2 19:11
 * @Description: 车系管理 表现层
 */
@Controller
public class CarModelController {
    @Autowired
    private CarModelService carModelService;

    //页面上需要品牌管理列表，这里引用
    @Autowired
    private CarMakeService carMakeService;

    /*********************************************************************************************************************
     * @Author:  王海新
     * @Date:  19:12  2020/11/2
     * @Version:  1.0.0
     * @Description:  分页查询
     */
    @RequestMapping("/sys/model/list")
    //不添加下面注解会报一下错误
    //Error resolving template [sys/make/list], template might not exist or might not be accessible by any of the configured Template Resolvers
    @ResponseBody
    public DataGridResult findByPage(QueryDTO queryDTO){
        return carModelService.findByPage(queryDTO);
    }

    /*********************************************************************************************************************
     * @Author:  王海新
     * @Date:  19:12  2020/11/2
     * @Version:  1.0.0
     * @Description:  删除
     */
    @RequestMapping("/sys/model/del")
    @ResponseBody
    public R delMake(@RequestBody List<Integer> ids){
        for (Integer id : ids) {
            carModelService.delCarModel(id);
        }
        return R.ok();
    }

    /*********************************************************************************************************************
     * @Author:  王海新
     * @Date:  19:19  2020/11/2
     * @Version:  1.0.0
     * @Description:  调用品牌service层，获取所有类别，在添加时用
     */
    @RequestMapping("/sys/model/typeall")
    @ResponseBody
    public R typeall(){
        List<CarMake> all = carMakeService.findAll();
        return R.ok().put("sites",all);
    }

    /*********************************************************************************************************************
     * @Author:  王海新
     * @Date: 19:12  2020/11/2
     * @Version:  1.0.0
     * @Description:  id查询
     */
    @RequestMapping("/sys/model/info/{id}")
    @ResponseBody
    public R findById(@PathVariable("id") Integer id){
        CarModel byId = carModelService.findById(id);
        return R.ok().put("model",byId);
    }

    /*********************************************************************************************************************
     * @Author:  王海新
     * @Date:  19:12  2020/11/2
     * @Version:  1.0.0
     * @Description:  添加方法
     */
    @RequestMapping("/sys/model/save")
    @ResponseBody
    public R addCarMake(@RequestBody CarModel carModel){
        int i = carModelService.addCarModel(carModel);
        return i>0 ? R.ok():R.error("添加失败");
    }

    /*********************************************************************************************************************
     * @Author:  王海新
     * @Date:  19:12  2020/11/2
     * @Version:  1.0.0
     * @Description:  修改方法
     */
    @RequestMapping("/sys/model/update")
    @ResponseBody
    public R upCarMake(@RequestBody CarModel carModel){
        int i = carModelService.upCarModel(carModel);
        return i>0 ? R.ok():R.error("修改失败");
    }

}
